package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.NoticeMapper;
import com.hghivln.pojo.domain.Notice;
import com.hghivln.pojo.dto.NoticeDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public AjaxResult listNoticeForPage(NoticeDto noticeDto) {
        Page<Notice> page = new Page<>(noticeDto.getPageNum(), noticeDto.getPageSize());
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(noticeDto.getNoticeTitle()), Notice.COL_NOTICE_TITLE, noticeDto.getNoticeTitle());
        qw.like(StringUtils.isNotBlank(noticeDto.getCreateBy()), Notice.COL_CREATE_BY, noticeDto.getCreateBy());
        qw.eq(StringUtils.isNotBlank(noticeDto.getNoticeType()), Notice.COL_NOTICE_TYPE, noticeDto.getNoticeType());
        qw.eq(noticeDto.getStatus() != null, Notice.COL_STATUS, noticeDto.getStatus());
        qw.eq(Notice.DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(Notice.COL_CREATE_TIME);
        noticeMapper.selectPage(page, qw);
        return AjaxResult.success("查询成功", page.getRecords(), page.getTotal());
    }

    @Override
    public AjaxResult addNotice(NoticeDto noticeDto) {
        try {
            Notice notice = new Notice();
            BeanUtils.copyProperties(noticeDto, notice);
            notice.setCreateBy(noticeDto.getSimpleUser().getUserName());
            notice.setCreateTime(DateUtil.date());
            return AjaxResult.success(noticeMapper.insert(notice));
        } catch (BeansException e) {
            e.printStackTrace();
            return AjaxResult.fail("添加失败");
        }
    }

    @Override
    public AjaxResult updateNotice(NoticeDto noticeDto) {
        try {
            Notice notice = new Notice();
            BeanUtils.copyProperties(noticeDto, notice);
            notice.setUpdateBy(noticeDto.getSimpleUser().getUserName());
            return AjaxResult.success(noticeMapper.updateById(notice));
        } catch (BeansException e) {
            e.printStackTrace();
            return AjaxResult.fail("更新失败");
        }
    }

    @Override
    public AjaxResult deleteNoticeByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        System.out.println("这是idList.size(){}" + idList.size());
        if (idList.size() > 0) {
            return AjaxResult.success(noticeMapper.deleteNoticeByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult getNoticeById(Long id) {
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.eq(Notice.DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Notice.COL_NOTICE_ID, id);
        return AjaxResult.success(noticeMapper.selectOne(qw));
    }
}
